package com.example.music_mediaplayer;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.music_mediaplayer.api.ILrcBuilder;
import com.example.music_mediaplayer.control.DefaultLrcBuilder;
import com.example.music_mediaplayer.globalvariable.GlobalVariable;
import com.example.music_mediaplayer.model.LrcRow;
import com.example.music_mediaplayer.model.MessageEvent;
import com.example.music_mediaplayer.observermodel.MyObservable;
import com.example.music_mediaplayer.redefineview.LrcView;
import com.example.music_mediaplayer.service.MyService;
import com.example.music_mediaplayer.tool.LogoutT;
import com.example.music_mediaplayer.tool.MsToTimeUtils;
import com.example.music_mediaplayer.tool.ToastTools;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by quan on 2018/1/15.
 */

public class PlayingActivity extends Activity implements View.OnClickListener,SeekBar.OnSeekBarChangeListener ,Observer{
    private ImageView img_back;
    private static TextView te_song_time_end;
    private  static TextView te_song_time_start;
    private boolean flg = true;
    private Intent intent;
    private static SeekBar seekBar;
    private static ImageView img_start_pause;
    private ImageView img_last;
    private ImageView img_next;
    private static TextView te_song_name;
    private static TextView te_song_artist;
    public static LrcView lrcView;
//    private ImageView img_state;
    private String msgg;
//    private int currentState = 1;
//    private int[] img_list = {R.drawable.random,R.drawable.single,R.drawable.list_play};

    private static String songName ;
    private static String songArtist;
    private String songLrc;
    private int seekBarMax;
    private int songTimeEnd ;
    private int seekBarProgress;
    public static List<LrcRow> rows = new ArrayList<>();

    private final static String MUSIC_PLAY_PAUSE = "music_play_pause";
    private final static String MUSIC_LAST = "music_last";
    private final static String MUSIC_NEXT = "music_next";

    private static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    seekBar.setProgress(msg.arg1);
                    te_song_time_start.setText(MsToTimeUtils.stringForTime(msg.arg1));
                    lrcView.seekLrcToTime(msg.arg1);
                    break;
                case 2:
                    te_song_name.setText(GlobalVariable.LocationSongList.get(msg.arg1).getSongName());
                    te_song_artist.setText(GlobalVariable.LocationSongList.get(msg.arg1).getSongArtist());
                    te_song_time_end.setText(MsToTimeUtils.stringForTime(GlobalVariable.duration));
                    ILrcBuilder builder = new DefaultLrcBuilder();
                    rows = builder.getLrcRows(GlobalVariable.LocationSongList.get(msg.arg1).getSongLyric());
                    lrcView.setLrc(rows);
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playing);
        initView();
        if (songLrc!=null && !songLrc.equals("")){
            ILrcBuilder builder = new DefaultLrcBuilder();
            rows = builder.getLrcRows(MyService.getSongLrc());
            lrcView.setLrc(rows);
        }
    }

    public void initView(){
        MyObservable.getInstance().addObserver(this);
        EventBus.getDefault().register(this);
        getData();
        lrcView = (LrcView) findViewById(R.id.lrcShowView);
        img_back = (ImageView) findViewById(R.id.back);
        img_back.setOnClickListener(this);
        te_song_time_end = (TextView) findViewById(R.id.song_time_end);
        te_song_time_start = (TextView) findViewById(R.id.song_time_start);
        te_song_name = (TextView) findViewById(R.id.song_name);
        te_song_name.setSelected(true);
        te_song_artist = (TextView) findViewById(R.id.artist);
        te_song_artist.setSelected(true);
        img_start_pause = (ImageView) findViewById(R.id.start_pause);
        if (MyService.isPlaying()){
            img_start_pause.setImageResource(R.drawable.pause);
        }
        img_start_pause.setOnClickListener(this);
        img_last = (ImageView) findViewById(R.id.last);
        img_last.setOnClickListener(this);
        img_next = (ImageView) findViewById(R.id.next);
        img_next.setOnClickListener(this);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(this);
//        img_state = (ImageView) findViewById(R.id.play_state);
//        img_state.setOnClickListener(this);
        te_song_name.setText(songName);
        te_song_artist.setText(songArtist);
        seekBar.setMax(seekBarMax);
        te_song_time_end.setText(MsToTimeUtils.stringForTime(GlobalVariable.duration));
        seekBar.setProgress(seekBarProgress);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        MyObservable.getInstance().deleteObserver(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){
        Message msg = new Message();
        msg.what = 1;
        msg.arg1 = messageEvent.getTime();
        handler.sendMessage(msg);
    }



    public void getData(){
        intent = getIntent();
        songLrc = intent.getStringExtra("songLrc");
//        LogoutT.logout(songLrc + "02020202020202");
        songName = intent.getStringExtra("song_name");
        songArtist = intent.getStringExtra("song_artist");
        seekBarMax = intent.getIntExtra("seekBar",0);
        songTimeEnd = intent.getIntExtra("songTime",0);
        seekBarProgress = intent.getIntExtra("currentTime",0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.start_pause:
                if (GlobalVariable.LocationSongList.size()==0){
                    ToastTools.toastMsg(this,"当前列表为空");
                }else {
                    if (flg){
                        img_start_pause.setImageResource(R.drawable.start);
                        msgg = MUSIC_PLAY_PAUSE;
                        intent = new Intent(this, MyService.class);
                        intent.putExtra("msg", msgg);
                        intent.putExtra("url", "");
                        intent.putExtra("songName","");
                        intent.putExtra("songArtist","");
                        intent.putExtra("songPhoto","");
                        startService(intent);
                        flg = false;
                    }else {
                        img_start_pause.setImageResource(R.drawable.pause);
                        msgg = MUSIC_PLAY_PAUSE;
                        intent = new Intent(this, MyService.class);
                        intent.putExtra("msg", msgg);
                        intent.putExtra("url", "");
                        intent.putExtra("songName","");
                        intent.putExtra("songArtist","");
                        intent.putExtra("songPhoto","");
                        startService(intent);
                        flg = true;
                    }
                }
                break;
             case R.id.last:
                 msgg = MUSIC_LAST;
                 intent = new Intent(this,MyService.class);
                 intent.putExtra("msg",msgg);
                 intent.putExtra("url","");
                 intent.putExtra("songName","");
                 intent.putExtra("songArtist","");
                 intent.putExtra("songPhoto","");
                 startService(intent);
                 break;
            case R.id.next:
                msgg = MUSIC_NEXT;
                intent = new Intent(this,MyService.class);
                intent.putExtra("msg",msgg);
                intent.putExtra("url","");
                intent.putExtra("songName","");
                intent.putExtra("songArtist","");
                intent.putExtra("songPhoto","");
                startService(intent);
                break;
//            case R.id.play_state:
//                currentState = currentState%img_list.length;
//                currentState++;
//                if (currentState == 1){
//                    img_state.setImageResource(R.drawable.random);
//                    intent = new Intent();
//                    intent.setAction("play_state");
//                    intent.putExtra("state","1");
//                    sendBroadcast(intent);
//                }else if(currentState == 2){
//                    img_state.setImageResource(R.drawable.list_play);
//                    intent = new Intent();
//                    intent.setAction("play_state");
//                    intent.putExtra("state","2");
//                    sendBroadcast(intent);
//                }else if (currentState == 3){
//                    img_state.setImageResource(R.drawable.single);
//                    intent = new Intent();
//                    intent.setAction("play_state");
//                    intent.putExtra("state","3");
//                    sendBroadcast(intent);
//                }
//                break;
            }
        }

    @Override
    protected void onResume() {
        super.onResume();
        MyObservable.getInstance().addObserver(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser){
            int currentProgress = seekBar.getProgress();
            intent = new Intent();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                //  大于等于24即为7.0及以上执行内容
                intent.setComponent(new ComponentName("com.example.music_mediaplayer","com.example.music_mediaplayer.broadcast.ServiceReceiver"));
            }
            intent.setAction("seekBarChange");
            intent.putExtra("currentProgress",currentProgress);
            sendBroadcast(intent);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }



    @Override
    public void update(Observable o, Object arg) {
        LogoutT.logout(arg+"   arg");
        Message msg = new Message();
        msg.what = 2;
        msg.arg1 = (int)arg;
        handler.sendMessage(msg);
    }

}
